package com.example.xhg;

import java.util.ArrayList;
import java.util.List;

import service_thing.Board;
import service_thing.Library;
import service_thing.Lost;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Service extends Fragment implements OnClickListener{
	private View v;
	
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragment;
	
	private RelativeLayout mTabLibrary;
	private RelativeLayout mTabLost;
	private RelativeLayout mTabBoard;
	
	private TextView mTabLibraryTextView;
	private TextView mTabLostTextView;
	private TextView mTabBoardTextView;
	
	private ImageView mLibraryImageView;
	private ImageView mLostImageView;
	private ImageView mBoardImageView;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v=inflater.inflate(R.layout.activity_service, null);
		intiView();
        intiEvent();
        setSelect(0);
        
        mAdapter=new FragmentPagerAdapter(getChildFragmentManager()) {
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mFragment.size();
			}
			
			@Override
			public Fragment getItem(int arg0) {
				// TODO Auto-generated method stub
				return mFragment.get(arg0);
			}
		};
		
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					setSelect(0);
					break;
				case 1:
					setSelect(1);
					break;
				case 2:
					setSelect(2);
					break;
				}				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		return v;
	}

	private void intiView() {
        mViewPager=(ViewPager) v.findViewById(R.id.id_service_viewpaper);
		 
		 mTabLibrary= (RelativeLayout)v.findViewById(R.id.id_tab_Library_layout);
    	  mTabLost=(RelativeLayout) v.findViewById(R.id.id_tab_Lost_layout);
    	  mTabBoard=(RelativeLayout) v.findViewById(R.id.id_tab_Board_layout);
    	  
    	  mTabLibraryTextView=(TextView) v.findViewById(R.id.id_Library_TextView);
      	  mTabLostTextView=(TextView)v.findViewById(R.id.id_Lost_TextView);
      	  mTabBoardTextView=(TextView) v.findViewById(R.id.id_Board_TextView);
      	  
      	  mLibraryImageView=(ImageView) v.findViewById(R.id.id_Library_ImageView);
      	  mLostImageView=(ImageView) v.findViewById(R.id.id_Lost_ImageView);
      	  mBoardImageView=(ImageView) v.findViewById(R.id.id_Board_ImageView);

      	  mFragment =new ArrayList<Fragment>();
      	  Fragment mLibrary = new service_thing.Library();
      	  Fragment mLost = new service_thing.Lost();
      	  Fragment mBoard = new service_thing.Board();
      	  
      	  mFragment.add(mLibrary);
      	  mFragment.add(mLost);
      	  mFragment.add(mBoard);

	}

	private void intiEvent() {
		mTabLibrary.setOnClickListener(this);
		mTabLost.setOnClickListener(this);
		mTabBoard.setOnClickListener(this);
	}

	private void setSelect(int i) {
		setTab(i);
		mViewPager.setCurrentItem(i);
	}

	private void setTab(int i) {
		// TODO Auto-generated method stub
		resetTab();
		switch (i) {
		case 0:
			mTabLibraryTextView.setTextColor(android.graphics.Color.RED);
			mLibraryImageView.setImageResource(R.drawable.tabsliding);

			break;
		case 1:
			mTabLostTextView.setTextColor(android.graphics.Color.RED);
			mLostImageView.setImageResource(R.drawable.tabsliding);

			break;
		case 2:
			mTabBoardTextView.setTextColor(android.graphics.Color.RED);
			mBoardImageView.setImageResource(R.drawable.tabsliding);

			break;
		}
		
	}

	private void resetTab() {
		mTabLibraryTextView.setTextColor(android.graphics.Color.WHITE);
		mTabLostTextView.setTextColor(android.graphics.Color.WHITE);
		mTabBoardTextView.setTextColor(android.graphics.Color.WHITE);
		
		mLibraryImageView.setImageResource(R.drawable.tabsliding_black);
		mLostImageView.setImageResource(R.drawable.tabsliding_black);
		mBoardImageView.setImageResource(R.drawable.tabsliding_black);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.id_tab_Library_layout:
			setSelect(0);
			break;
		case R.id.id_tab_Lost_layout:
			setSelect(1);
			break;
		case R.id.id_tab_Board_layout:
			setSelect(2);
			break;
		}
	}

	
}
